using System;
using System.Activities;
using System.Text;
using Headsight.BPMN2WF.BPMN.Model;
using Headsight.BPMN2WF.BPMN.Schemas;
using Headsight.BPMN2WF.WF.Activities;

namespace Headsight.BPMN2WF.Transformation.Transformers
{
    /// <summary>
    /// <see cref="IBPMN2WFTransformation"/> mapping
    /// <see cref="tFlowElement"/> to <i>UserTask</i>.
    /// </summary>
    public class BPMN2WFTransformationCustomWFUserTask : IBPMN2WFTransformation
    {
        // This is some kind of callback invoked form the BPMN2WF core transformation engine.
        // Anyway the following API is still under development and subject of change... 
        // The API still looks to noisy...
        public BPMNTransformationResult Transform(IBPMN2WFTransformationContext ctx, BPMNFlowNode flowNode)
        {
            var result = new UserTask { Text = flowNode.TElement.name };

            // -> Retrieve our BPMN 2.0 flowElement's (actually a BPMN 2.0 user task) in-arguments
            var argsIn = flowNode.ArgumentsIn;

            // Do something meaningful with the BPMN flowElement's in-arguments information, i.e., 
            // transform them into WF4.x counterparts on the resulting WF4.x activity.
            // The following is - of course - no production ready argument transformation, but...
            var argsInEnc = new StringBuilder();
            foreach (var each in argsIn)
            {
                argsInEnc.Append(each.Id).Append(": ").Append(each.Expression);
                argsInEnc.Append("##;;##");
            }
            result.EncodedParameters = new InArgument<String>(argsInEnc.ToString());

            return new BPMNTransformationResult(result, result, result);
        }
    }
}